package win.smartown.mistyle.base;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewStub;

import win.smartown.mistyle.demo.R;
import win.smartown.mistyle.viewController.loading.LoadingEmptyController;
import win.smartown.mistyle.viewController.loading.LoadingEmptyInterface;
import win.smartown.mistyle.viewController.loading.LoadingViewController;
import win.smartown.mistyle.viewController.loading.LoadingViewInterface;

/**
 * Created by smartown on 2017/3/18.
 * <br>
 * Description:带有loading提示的activity
 */
public class LoadingActivity extends BaseActivity implements LoadingViewInterface, LoadingEmptyInterface {

    private View contentView;
    private LoadingEmptyInterface loadingEmpty;
    private LoadingViewInterface loadingView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseView(R.layout.activity_loading);
        loadingView = setupLoadingView((ViewStub) super.findView(R.id.loading_loading));
        loadingEmpty = setupLoadingEmptyView((ViewStub) super.findView(R.id.loading_unusual));
    }

    protected final void setContent(@LayoutRes int id) {
        ViewStub viewStub = super.findView(R.id.loading_content);
        viewStub.setLayoutResource(id);
        contentView = viewStub.inflate();
    }

    protected <T extends View> T findView(View rootView, @IdRes int id) {
        return (T) rootView.findViewById(id);
    }

    protected <T extends View> T findView(@IdRes int id) {
        return findView(contentView, id);
    }

    protected LoadingViewInterface setupLoadingView(ViewStub loadingViewStub) {
        return new LoadingViewController(loadingViewStub);
    }

    @Override
    public void startLoading() {
        loadingView.startLoading();
    }

    @Override
    public void finishLoading() {
        loadingView.finishLoading();
    }

    protected LoadingEmptyInterface setupLoadingEmptyView(ViewStub emptyViewStub) {
        return new LoadingEmptyController(emptyViewStub);
    }

    @Override
    public void resetData() {
        loadingEmpty.resetData();
    }

    @Override
    public void noData() {
        loadingEmpty.noData();
    }

}
